package findix.SortingAlgotithm;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.BorderLayout;

import javax.swing.JPanel;

import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.UIManager;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.TextField;
import java.awt.event.TextListener;
import java.awt.event.TextEvent;
import java.util.Random;
import javax.swing.JCheckBox;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

public class Windows {
	TextField textField_insert = null;
	TextField textField_shell = null;
	TextField textField_bubble = null;
	TextField textField_quick = null;
	TextField textField_select = null;
	TextField textField_heap = null;
	TextField textField_merge = null;
	TextField textField_ArryLength = null;

	JLabel lblTimeLabel_insert = null;
	JLabel lblTimeLabel_shell = null;
	JLabel lblTimeLabel_bubble = null;
	JLabel lblTimeLabel_quick = null;
	JLabel lblTimeLabel_select = null;
	JLabel lblTimeLabel_heap = null;
	JLabel lblTimeLabel_merge = null;

	private JCheckBox CheckBox;

	private JFrame frame;

	long time[] = new long[7];
	boolean isChecked = false;

	int LENTGH_OF_ARRAY = 10000;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Windows window = new Windows();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Windows() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {

		frame = new JFrame();
		frame.setBounds(200, 100, 800, 600);
		frame.setTitle("各种排序算法时间性能的比较©凤翔");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new GridLayout(0, 1, 0, 0));

		JPanel panel_main = new JPanel();
		frame.getContentPane().add(panel_main);
		panel_main.setLayout(new GridLayout(8, 0, 20, 10));

		JPanel panel_button = new JPanel();
		panel_button.setBorder(UIManager
				.getBorder("RootPane.fileChooserDialogBorder"));
		panel_main.add(panel_button);

		JButton button_sequence = new JButton("正序初始排列");
		button_sequence.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (!isChecked)
					resetColor();
				StringBuffer arr = new StringBuffer();
				for (int i = 1; i <= LENTGH_OF_ARRAY; i++) {
					arr.append(i + " ");
				}
				textField_insert.setText(arr.substring(0, arr.length() - 1));
				textField_shell.setText(arr.substring(0, arr.length() - 1));
				textField_bubble.setText(arr.substring(0, arr.length() - 1));
				textField_quick.setText(arr.substring(0, arr.length() - 1));
				textField_select.setText(arr.substring(0, arr.length() - 1));
				textField_heap.setText(arr.substring(0, arr.length() - 1));
				textField_merge.setText(arr.substring(0, arr.length() - 1));
				if (isChecked)
					startSort();
			}
		});

		JButton button_reverse = new JButton("逆序初始排列");
		button_reverse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (!isChecked)
					resetColor();
				StringBuffer arr = new StringBuffer();
				for (int i = LENTGH_OF_ARRAY; i >= 1; i--) {
					arr.append(i + " ");
				}
				textField_insert.setText(arr.substring(0, arr.length() - 1));
				textField_shell.setText(arr.substring(0, arr.length() - 1));
				textField_bubble.setText(arr.substring(0, arr.length() - 1));
				textField_quick.setText(arr.substring(0, arr.length() - 1));
				textField_select.setText(arr.substring(0, arr.length() - 1));
				textField_heap.setText(arr.substring(0, arr.length() - 1));
				textField_merge.setText(arr.substring(0, arr.length() - 1));
				if (isChecked)
					startSort();
			}
		});

		JButton button_random = new JButton("随机初始排列");
		button_random.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!isChecked)
					resetColor();
				String tmp = getRandomArray();
				textField_insert.setText(tmp);
				textField_shell.setText(tmp);
				textField_bubble.setText(tmp);
				textField_quick.setText(tmp);
				textField_select.setText(tmp);
				textField_heap.setText(tmp);
				textField_merge.setText(tmp);
				if (isChecked)
					startSort();
			}
		});

		JLabel label = new JLabel("排序数组长度：");
		panel_button.add(label);

		textField_ArryLength = new TextField();
		textField_ArryLength.setBackground(UIManager.getColor("Button.shadow"));
		textField_ArryLength.setColumns(5);
		textField_ArryLength.addTextListener(new TextListener() {
			public void textValueChanged(TextEvent arg0) {
				LENTGH_OF_ARRAY = (int) Double.parseDouble(textField_ArryLength
						.getText());
			}
		});
		textField_ArryLength.setText(LENTGH_OF_ARRAY + "");
		panel_button.add(textField_ArryLength);
		panel_button.add(button_sequence);

		panel_button.add(button_reverse);

		panel_button.add(button_random);

		JButton button_start = new JButton("开始排序");
		button_start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				startSort();
			}
		});
		panel_button.add(button_start);

		CheckBox = new JCheckBox("自动排序");
		CheckBox.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				isChecked = !isChecked;
			}
		});
		panel_button.add(CheckBox);

		JPanel panel_insert = new JPanel();
		panel_insert.setBorder(UIManager
				.getBorder("RootPane.fileChooserDialogBorder"));
		panel_main.add(panel_insert);
		panel_insert.setLayout(new BorderLayout(0, 0));

		JLabel lblLabel_insert = new JLabel("插入排序：");
		panel_insert.add(lblLabel_insert, BorderLayout.WEST);

		textField_insert = new TextField("");
		panel_insert.add(textField_insert, BorderLayout.CENTER);

		lblTimeLabel_insert = new JLabel("");
		panel_insert.add(lblTimeLabel_insert, BorderLayout.EAST);

		JPanel panel_shell = new JPanel();
		panel_shell.setBorder(UIManager
				.getBorder("RootPane.questionDialogBorder"));
		panel_main.add(panel_shell);
		panel_shell.setLayout(new BorderLayout(0, 0));

		JLabel lblLabel_shell = new JLabel("希尔排序：");
		panel_shell.add(lblLabel_shell, BorderLayout.WEST);

		textField_shell = new TextField();
		panel_shell.add(textField_shell, BorderLayout.CENTER);

		lblTimeLabel_shell = new JLabel("");
		panel_shell.add(lblTimeLabel_shell, BorderLayout.EAST);

		JPanel panel_bubble = new JPanel();
		panel_bubble.setBorder(UIManager
				.getBorder("RootPane.questionDialogBorder"));
		panel_main.add(panel_bubble);
		panel_bubble.setLayout(new BorderLayout(0, 0));

		JLabel lblLabel_bubble = new JLabel("冒泡排序：");
		panel_bubble.add(lblLabel_bubble, BorderLayout.WEST);

		textField_bubble = new TextField();
		panel_bubble.add(textField_bubble);

		lblTimeLabel_bubble = new JLabel("");
		panel_bubble.add(lblTimeLabel_bubble, BorderLayout.EAST);

		JPanel panel_quick = new JPanel();
		panel_quick.setBorder(UIManager
				.getBorder("RootPane.questionDialogBorder"));
		panel_main.add(panel_quick);
		panel_quick.setLayout(new BorderLayout(0, 0));

		JLabel lblLabel_quick = new JLabel("快速排序：");
		panel_quick.add(lblLabel_quick, BorderLayout.WEST);

		textField_quick = new TextField();
		panel_quick.add(textField_quick);

		lblTimeLabel_quick = new JLabel("");
		panel_quick.add(lblTimeLabel_quick, BorderLayout.EAST);

		JPanel panel_select = new JPanel();
		panel_select.setBorder(UIManager
				.getBorder("RootPane.questionDialogBorder"));
		panel_main.add(panel_select);
		panel_select.setLayout(new BorderLayout(0, 0));

		JLabel lblLabel_select = new JLabel("选择排序：");
		panel_select.add(lblLabel_select, BorderLayout.WEST);

		textField_select = new TextField();
		panel_select.add(textField_select);

		lblTimeLabel_select = new JLabel("");
		panel_select.add(lblTimeLabel_select, BorderLayout.EAST);

		JPanel panel_heap = new JPanel();
		panel_heap.setBorder(UIManager
				.getBorder("RootPane.questionDialogBorder"));
		panel_main.add(panel_heap);
		panel_heap.setLayout(new BorderLayout(0, 0));

		JLabel lblLabel_heap = new JLabel("堆排序   ：");
		panel_heap.add(lblLabel_heap, BorderLayout.WEST);

		textField_heap = new TextField();
		panel_heap.add(textField_heap);

		lblTimeLabel_heap = new JLabel("");
		panel_heap.add(lblTimeLabel_heap, BorderLayout.EAST);

		JPanel panel_merge = new JPanel();
		panel_merge.setBorder(UIManager
				.getBorder("RootPane.questionDialogBorder"));
		panel_main.add(panel_merge);
		panel_merge.setLayout(new BorderLayout(0, 0));

		JLabel lblLabel_merge = new JLabel("归并排序：");
		panel_merge.add(lblLabel_merge, BorderLayout.WEST);

		textField_merge = new TextField();
		panel_merge.add(textField_merge);

		lblTimeLabel_merge = new JLabel("");
		panel_merge.add(lblTimeLabel_merge, BorderLayout.EAST);
	}

	String getRandomArray() {
		int[] check = new int[LENTGH_OF_ARRAY + 1];
		StringBuffer arr = new StringBuffer();
		int tmp = 0;
		Random ran = new Random();
		for (int i = 1; i <= LENTGH_OF_ARRAY; i++) {
			tmp = ran.nextInt(LENTGH_OF_ARRAY) + 1;
			if (check[tmp] == 0) {
				arr.append(tmp + " ");
				check[tmp] = 1;
			} else
				i--;
		}
		return arr.substring(0, arr.length() - 1);
	}

	int[] getIntArray(String str) {
		String[] tmp = str.split(" ");
		// System.out.print(tmp.length);
		int[] tmpInt = new int[tmp.length];
		for (int i = 0; i < tmp.length; i++) {
			tmpInt[i] = Integer.parseInt(tmp[i]);
			// System.out.print(tmp[i]);
		}
		return tmpInt;
	}

	String getString(int[] intArray) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < intArray.length; i++) {
			sb.append(intArray[i] + (i == intArray.length - 1 ? "" : " "));
			// System.out.println(i+" "+intArray[i]);
		}
		return sb.toString();
	}

	void highLight() {
		String[] color = { "#ff3d55", "#ff5e56", "#ff7f47", "#ff9058",
				"#ffb169", "#ffd2a0", "#fff3c1" };
		int max = -1;
		int maxLocation = 0;
		int last = -1;
		int lastI = 0;
		for (int i = 0; i < time.length; i++) {
			for (int j = 0; j < time.length; j++) {
				if (time[j] >= max) {
					max = (int) time[j];
					maxLocation = j;
				}
			}
			if (last == max) {
				setColor(maxLocation, Color.decode(color[lastI]));
			} else {
				setColor(maxLocation, Color.decode(color[i]));
				last = max;
				lastI = i;
			}
			time[maxLocation] = -1;
			max = 0;
			maxLocation = 0;
		}
	}

	void resetColor() {
		Color color = Color.white;
		textField_insert.setBackground(color);
		textField_shell.setBackground(color);
		textField_bubble.setBackground(color);
		textField_quick.setBackground(color);
		textField_select.setBackground(color);
		textField_heap.setBackground(color);
		textField_merge.setBackground(color);
	}

	void setColor(int location, Color color) {
		switch (location) {
		case 0:
			textField_insert.setBackground(color);
			break;
		case 1:
			textField_shell.setBackground(color);
			break;
		case 2:
			textField_bubble.setBackground(color);
			break;
		case 3:
			textField_quick.setBackground(color);
			break;
		case 4:
			textField_select.setBackground(color);
			break;
		case 5:
			textField_heap.setBackground(color);
			break;
		case 6:
			textField_merge.setBackground(color);
			break;
		}
	}

	void startSort() {
		if (textField_insert.getText().isEmpty())
			return;
		Sort sort = new Sort();
		long start = System.currentTimeMillis();
		textField_insert.setText(getString(sort
				.insertSort(getIntArray(textField_insert.getText()))));

		time[0] = System.currentTimeMillis() - start;
		lblTimeLabel_insert.setText("消耗时间为：" + time[0] + "毫秒");
		start = System.currentTimeMillis();
		textField_shell.setText(getString(sort
				.shellSort(getIntArray(textField_shell.getText()))));

		time[1] = System.currentTimeMillis() - start;
		lblTimeLabel_shell.setText("消耗时间为：" + time[1] + "毫秒");
		start = System.currentTimeMillis();
		textField_bubble.setText(getString(sort
				.bubbleSort(getIntArray(textField_bubble.getText()))));

		time[2] = System.currentTimeMillis() - start;
		lblTimeLabel_bubble.setText("消耗时间为：" + time[2] + "毫秒");
		start = System.currentTimeMillis();
		textField_quick.setText(getString(sort
				.quickSort(getIntArray(textField_quick.getText()))));

		time[3] = System.currentTimeMillis() - start;
		lblTimeLabel_quick.setText("消耗时间为：" + time[3] + "毫秒");
		start = System.currentTimeMillis();
		textField_select.setText(getString(sort
				.selectSort(getIntArray(textField_select.getText()))));

		time[4] = System.currentTimeMillis() - start;
		lblTimeLabel_select.setText("消耗时间为：" + time[4] + "毫秒");
		start = System.currentTimeMillis();
		textField_heap.setText(getString(sort
				.heapSort(getIntArray(textField_heap.getText()))));

		time[5] = System.currentTimeMillis() - start;
		lblTimeLabel_heap.setText("消耗时间为：" + time[5] + "毫秒");
		start = System.currentTimeMillis();
		textField_merge.setText(getString(sort
				.mergeSort(getIntArray(textField_merge.getText()))));

		time[6] = System.currentTimeMillis() - start;
		lblTimeLabel_merge.setText("消耗时间为：" + time[6] + "毫秒");
		highLight();

	}
}
